//
//  EOCNetworkFetcher.m
//  MacTestProject
//
//  Created by LiuLichen on 2022/4/26.
//

#import "EOCNetworkFetcher.h"

@interface EOCNetworkFetcher ()

@property (nonatomic, strong, readwrite) NSURL* url;
@property (nonatomic, copy) EOCNetworkFetcherCompletionHandler completionHanlder;
@property (nonatomic, strong) NSData* downloadeddata;

@end

@implementation EOCNetworkFetcher

- (void) receiveData {
    NSData* data = [NSData new];
    if ([_delegate respondsToSelector: @selector(networkFetcher:didReceiveData:)]) {
        [_delegate networkFetcher:self didReceiveData:data];
    }
}

- (id) initWithURL:(NSURL *)url {
    if ((self = [super init])) {
        _url = url;
    }
    return self;
}

- (void) startWithCompletionHandler:(EOCNetworkFetcherCompletionHandler)handler {
    self.completionHanlder = handler;
    // Start the request, request sets downloadedData property.
    // When request is finished, p_requestCompleted is called.
    [self p_requestCompleted];
}

- (void) p_requestCompleted {
    if (_completionHanlder) {
        _completionHanlder(_downloadeddata);
    }
}

@end
